#include <cstdlib>
#include <cstdio>
#include <iostream>
#include "minIni.h"
#include "config.h"

using namespace Oribi;

ConfigFile::ConfigFile(std::string fname)
{
    ini = new minIni(fname);
    fileName = fname;
}

int ConfigFile::geti(std::string section, std::string key, int defVal)
{
    if (!ini)
        return defVal;
    return ini->geti(section, key, defVal);
}

long ConfigFile::getl(std::string section, std::string key, long defVal)
{
    if (!ini)
        return defVal;
    return ini->getl(section, key, defVal);
}

float ConfigFile::getf(std::string section, std::string key, float defVal)
{
    if (!ini)
        return defVal;
    return ini->getf(section, key, defVal);
}

bool ConfigFile::getb(std::string section, std::string key, bool defVal)
{
    if (!ini)
        return defVal;
    return ini->getbool(section, key, defVal);
}
#include "entityaction.h"
std::string ConfigFile::gets(std::string section, std::string key, std::string defVal)
{
    if (!ini)
        return defVal;
    return ini->gets(section, key, defVal);
}

std::string ConfigFile::getFileName() const
{
    return fileName;
}

void ConfigFile::setSourceFile(std::string fname)
{
    delete ini;
    ini = new minIni(fname);
    fileName = fname;
}

bool ConfigFile::put(std::string section, std::string key, int val)
{
    return ini->put(section, key, val);
}

bool ConfigFile::put(std::string section, std::string key, long val)
{
    return ini->put(section, key, val);
}

bool ConfigFile::put(std::string section, std::string key, float val)
{
    return ini->put(section, key, val);
}

bool ConfigFile::put(std::string section, std::string key, bool val)
{
    return ini->put(section, key, val);
}

bool ConfigFile::put(std::string section, std::string key, std::string val)
{
    return ini->put(section, key, val);
}

/* The configuration context, holding all the
 * config files and the information read from them */

ConfContext::ConfContext(){}

#define N_CONFIG_FILES 1
static std::string configFileNames[N_CONFIG_FILES] =
{
    "conf/confmain.ini"
};

int ConfContext::initialise()
{
    for (int i = 0; i < N_CONFIG_FILES; ++i)
    {
        files.push_back(ConfigFile(configFileNames[i]));
    }
    return 0;
}

ConfigFile *ConfContext::getFile(std::string name)
{
    for (unsigned i = 0; i < files.size(); ++i)
    {
        if (files[i].getFileName() == name)
            return &files[i];
    }
    return NULL;
}

ConfigFile *ConfContext::addFile(std::string name)
{
    files.push_back(ConfigFile(name));
    return &files.back();
}

int ConfContext::geti(std::string file, std::string section, std::string key, int defVal)
{
    ConfigFile *f = getFile(file);
    if (!f)
        return defVal;

    return f->geti(section, key, defVal);
}

long ConfContext::getl(std::string file, std::string section, std::string key, long defVal)
{
    ConfigFile *f = getFile(file);
    if (!f)
        return defVal;

    return f->getl(section, key, defVal);
}

float ConfContext::getf(std::string file, std::string section, std::string key, float defVal)
{
    ConfigFile *f = getFile(file);
    if (!f)
        return defVal;

    return f->getf(section, key, defVal);
}

bool ConfContext::getb(std::string file, std::string section, std::string key, bool defVal)
{
    ConfigFile *f = getFile(file);
    if (!f)
        return defVal;

    return f->getb(section, key, defVal);
}

std::string ConfContext::gets(std::string file, std::string section, std::string key, std::string defVal)
{
    ConfigFile *f = getFile(file);
    if (!f)
        return defVal;

    return f->gets(section, key, defVal);
}

bool ConfContext::put(std::string file, std::string section, std::string key, int val)
{
    ConfigFile *f = getFile(file);
    if (!f)
        return false;

    return f->put(section, key, val);
}

bool ConfContext::put(std::string file, std::string section, std::string key, long val)
{
    ConfigFile *f = getFile(file);
    if (!f)
        return false;

    return f->put(section, key, val);
}

bool ConfContext::put(std::string file, std::string section, std::string key, float val)
{
    ConfigFile *f = getFile(file);
    if (!f)
        return false;

    return f->put(section, key, val);
}

bool ConfContext::put(std::string file, std::string section, std::string key, bool val)
{
    ConfigFile *f = getFile(file);
    if (!f)
        return false;

    return f->put(section, key, val);
}

bool ConfContext::put(std::string file, std::string section, std::string key, std::string val)
{
    ConfigFile *f = getFile(file);
    if (!f)
        return false;

    return f->put(section, key, val);
}
